package weaver.interfaces.zhlx.duss.action.financemodule.t6;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.T6Util;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-27
 * @Description: 资金计划增补/提前支付审批流程调用接口追加资金系统
 * @ModifyLog:
 **/
public class WeaZjjhzbTqzfsplcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaZjjhzbTqzfsplcAction>>>>>requestid=" + requestid);
        try {
            String message = "调用资金系统接口返回:";
            RecordSet rs = new RecordSet();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            JSONArray paramList = new JSONArray();
            rs.executeQuery("select * from " + tablename + "_dt1 where mainid=? and nvl(type,1) !=2", mainid);
            while (rs.next()) {
                JSONObject dataJson = new JSONObject();
                dataJson.put("DETAIL_ID", Util.null2String(rs.getString("jhid")));
                dataJson.put("DATA_SOURCE", "EC");
                String ly = Util.null2String(rs.getString("ly"));
                if ("1".equals(ly) || "3".equals(ly)) {
                    dataJson.put("CD_SIGN", "0");
                }
                if ("0".equals(ly) || "2".equals(ly)) {
                    dataJson.put("CD_SIGN", "1");
                }
                dataJson.put("DETAIL_TEXT", Util.null2String(rs.getString("zjmxxmwb")));
                dataJson.put("ITEM_CODE", Util.null2String(rs.getString("ysbm")));
                dataJson.put("ITEM_NAME", Util.null2String(rs.getString("yskm")));
                dataJson.put("CORP_CODE", Util.null2String(rs.getString("gsdm")));
                dataJson.put("CORP_NAME", Util.null2String(rs.getString("gsmc")));
                dataJson.put("CS_CODE", Util.null2String(rs.getString("gysbm")));
                dataJson.put("CS_NAME", Util.null2String(rs.getString("gysmc")));
                String zffs = Util.null2String(rs.getString("zffs"));
                dataJson.put("VOUCHER_TYPE", "1".equals(zffs) ? "70" : "06");
                dataJson.put("CUR", Util.null2String(rs.getString("bz")));
                dataJson.put("ABS", Util.null2String(rs.getString("jyzfnr")));
                dataJson.put("BUSINESS_USER", Util.null2String(rs.getString("ywqrr")));
                dataJson.put("DEPT_NAME", Util.null2String(rs.getString("tbbm")));
                dataJson.put("AMOUNT_Y", Util.null2String(rs.getString("cwfhjeyb")));
                dataJson.put("AMOUNT_B", Util.null2String(rs.getString("cwfhjebb")));
                dataJson.put("PAYEE_BANK1", Util.null2String(rs.getString("khyxdh")));
                dataJson.put("PAYEE_BANK_CODE1", Util.null2String(rs.getString("lxhdh")));
                dataJson.put("PAYEE_ACC_NO1", Util.null2String(rs.getString("yxzhdh")));
                dataJson.put("PAYEE_BANK2", Util.null2String(rs.getString("khyx")));
                dataJson.put("PAYEE_BANK_CODE2", Util.null2String(rs.getString("lxh")));
                dataJson.put("PAYEE_ACC_NO2", Util.null2String(rs.getString("yxzhcd")));
                dataJson.put("IS_BATCH", Util.null2String(rs.getString("pldb")));
                paramList.add(dataJson);
            }
            if (!paramList.isEmpty()) {
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("jkdymc", "WeaZjjhzbTqzfsplcAction");
                dataMap.put("lcid", requestid);
                dataMap.put("dylc", requestid);
                dataMap.put("cz", src);
                writeLog(">>>>>WeaZjjhzbTqzfsplcAction>>>>>paramList=" + paramList);
                JSONObject resultJson = T6Util.saveFundingPlan(paramList, dataMap);
                writeLog(">>>>>WeaZjjhzbTqzfsplcAction>>>>>resultJson=" + resultJson);
                String code = Util.null2String(resultJson.getString("code"));
                if ("success".equals(code)) {
                    JSONObject retrunData = JSONObject.parseObject(resultJson.getString("data"));
                    message += Util.null2String(retrunData.getString("MESSAGE"));
                    String data = Util.null2String(retrunData.getString("DATA"));
                    JSONArray dataList = JSONArray.parseArray(data);
                    int count = 0;
                    for (int i = 0; i < dataList.size(); i++) {
                        JSONObject dataJson = dataList.getJSONObject(i);
                        String code1 = Util.null2String(dataJson.getString("CODE"));
                        String msg = Util.null2String(dataJson.getString("MSG"));
                        if (!"2".equals(code1)) {
                            message += msg + ";";
                            count++;
                        } else {
                            String detailId = Util.null2String(dataJson.getString("DETAIL_ID"));
                            rs.executeQuery("update " + tablename + "_dt1 set type=? where mainid=? and jhid=?", code1, mainid, detailId);
                        }
                    }
                    if (count > 0) {
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                } else {
                    message += Util.null2String(resultJson.getString("message"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaZjjhzbTqzfsplcAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}